//
//  WJScrollView.swift
//  无线轮播图
//
//  Created by w s j on 16/6/6.
//  Copyright © 2016年 w s j. All rights reserved.
//

import UIKit

let width = UIScreen.mainScreen().bounds.width

class WJScrollView: UIView {
     var timer:NSTimer?
     var dataArray:NSArray?{
        didSet{
            /**
            *  因为传进来的是Array类型的数组，没有exchangeObjectAtIndex方法，只能转换为NSMutableArray类型，笔者初学swfit只能这样遍历一下了
            */
            for object in dataArray!{
                self.imageArray.addObject(object)
            }
            leftImageView.image = UIImage(named: self.imageArray[0]as! String)
            contentImageView.image = UIImage(named: self.imageArray[1]as! String )
            rightImageView.image = UIImage(named: self.imageArray[2] as! String)
            pageView.numberOfPages = self.imageArray.count
        }
    }
/// pageView当前点的颜色
    var cuttentPageColor:UIColor = UIColor.blueColor(){
        didSet{
            pageView.currentPageIndicatorTintColor = cuttentPageColor
        }
    }
///  pageView没有选中点的颜色
    var pageIndicatorTintColor:UIColor = UIColor.yellowColor(){
        didSet{
            pageView.pageIndicatorTintColor = pageIndicatorTintColor;
        }
    }
     override init(frame: CGRect) {
        super.init(frame: frame)
        steUpUI()
    }

    private func steUpUI(){
        
        addSubview(scrollView)
        scrollView.addSubview(leftImageView)
        scrollView.addSubview(contentImageView)
        scrollView.addSubview(rightImageView)
        addSubview(pageView)
        
        timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "guandong", userInfo: nil, repeats: true)
        NSRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
    }
    // 定时器和button的点击的时间前面都不能使用private修饰，因为他们的时间是runloop循环式调用
    func guandong()
    {
        scrollView.setContentOffset(CGPointMake(2.0*width, 0), animated: true)
    }
    /// 懒加载scrollview
    private lazy var scrollView :UIScrollView = {
        let scro = UIScrollView(frame: CGRectMake(0, 0, width, self.frame.size.height))
        scro.showsHorizontalScrollIndicator = false;
        scro.showsVerticalScrollIndicator = false;
        scro.contentSize = CGSizeMake(3.0*width, 0)
        scro.contentOffset = CGPointMake(width, 0)
        scro.delegate = self
        scro.bounces = false //设置弹簧效果关闭，有助于滚动的时候显示后面的白框
        scro.pagingEnabled = true // 设置分页效果，有助于手动拖动一页
        return scro
        }()
    
/// 懒加载左视图
    private lazy var leftImageView:UIImageView = {
        let left = UIImageView(frame: CGRectMake(0, 0, width, self.frame.size.height))
        
        return left
        }()
/// 懒加载当前视图
    private lazy var contentImageView:UIImageView = {
        let content = UIImageView(frame: CGRectMake(width, 0, width, self.frame.size.height))
        return content
        }()
/// 懒加载右视图
    private lazy var rightImageView:UIImageView = {
        let right = UIImageView(frame: CGRectMake(2.0*width, 0, width, self.frame.size.height))
        return right
        }()
/// 懒加载数据
    /*
    * :NSMutableArray使用可变数组类型为NSMutableArray，因为下面用到NSMutableArray里面的交换元素位置的方法
    */
    private lazy var imageArray :NSMutableArray = NSMutableArray()
/// 懒加载pageView
    private lazy var pageView:UIPageControl = {
        let page = UIPageControl(frame: CGRectMake((width - 200.0)*0.5, self.frame.size.height-20, 200, 20))
        page.currentPage = 0;
        page.currentPageIndicatorTintColor = self.cuttentPageColor
        page.pageIndicatorTintColor = self.pageIndicatorTintColor
        return page
        }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
// MARK: - scrollView的代理方法
extension WJScrollView:UIScrollViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        /**
        *  scrollview滚动到右视图的时候
        */
        if scrollView.contentOffset.x/width == 2.0
        {
            if pageView.currentPage == imageArray.count-1{
                pageView.currentPage = 0
            }else{
                pageView.currentPage = pageView.currentPage+1
            }
            for  i in 0..<imageArray.count-1 {
                // 交换数组里面元素的位置 每个都后移一个位置
                imageArray .exchangeObjectAtIndex(i, withObjectAtIndex: i+1)
            }
            leftImageView.image = UIImage(named: imageArray[0] as! String)
            contentImageView.image = UIImage(named: imageArray[1]as! String )
            rightImageView.image = UIImage(named: imageArray[2] as! String)
            // 滚动到了右视图后，让它不带动画再悄悄滚动到中间的视图
            scrollView.setContentOffset(CGPointMake(width, 0), animated: false)
        }
        // 向左滚动一个位置
        if scrollView.contentOffset.x/width == 0.0{
            if pageView.currentPage == 0{
                pageView.currentPage = imageArray.count-1
            }else{
                pageView.currentPage = pageView.currentPage-1
            }
            for i in 0..<imageArray.count-1 {
                // 将每个元素和最后一个元素进行交换
                imageArray.exchangeObjectAtIndex(i, withObjectAtIndex: imageArray.count-1)
            }
            leftImageView.image = UIImage(named: imageArray[0] as! String)
            contentImageView.image = UIImage(named: imageArray[1] as! String)
            rightImageView.image = UIImage(named: imageArray[2] as! String)
            // 还是一样的悄悄的滚到中间的位置
            scrollView.setContentOffset(CGPointMake(width, 0), animated: false)
        }
        
    }
    /**
    将要拖动的时候调用
    */
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        timer?.invalidate()
        timer = nil
    }
    /**
    拖动完成之后调用
    */
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)  {
        
        timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "guandong", userInfo: nil, repeats: true)
        NSRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
    }
}
